package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.page.RespPageBean;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.pojo.EmployeeDataBase;
import com.xxxx.server.pojo.Position;
import com.xxxx.server.service.IEmployeeService;
import com.xxxx.server.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Resource
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPositionService positionService;

    //展示所有数据
    @Override
    public RespPageBean getEmployeeByPage(Integer currentPage, Integer size, Employee employee, Date[] beginDateScope) {
        if (currentPage != null && size != null) {
            currentPage = (currentPage - 1) * size;
        }
        List<Employee> data = employeeMapper.getEmployeeByPage(currentPage, size, employee, beginDateScope);

        Long total = employeeMapper.getTotal(employee, beginDateScope);
        RespPageBean bean = new RespPageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    //删除数据
    @Override
    public Integer deleteEmployeeById(Integer id) {
        int i = employeeMapper.deleteById(id);
        return i;
    }

    //添加员工
    @Override
    public Integer saveEmployee(EmployeeDataBase employeeDataBase){
        LocalDate endContract = employeeDataBase.getEndContract();
        LocalDate beginContract = employeeDataBase.getBeginContract();

        //合同期限
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = sdf.parse(String.valueOf(beginContract));
            date2 = sdf.parse(String.valueOf(endContract));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long l = date2.getTime()- date1.getTime();
        long day = l / (24 * 60 * 60 * 1000);
            if (day <366){

                double i = day/365.00;
                java.math.BigDecimal   bd   =   new BigDecimal(i);
                bd = bd.setScale(2, BigDecimal.ROUND_FLOOR);
                double v = bd.doubleValue();
                //设置期限
                employeeDataBase.setContractTerm(v);
            }else {
                Double year = new Double(day/365);
                //设置期限
                employeeDataBase.setContractTerm(year);
            }
        //设置 id
        int workid = Integer.parseInt(employeeDataBase.getWorkID());
        employeeDataBase.setId(workid);
        //插入
        int result = employeeMapper.EmployeeInsert(employeeDataBase);
        return result;
    }


    //更新员工
    public Integer updateEmployeeById(Employee employee){
        return employeeMapper.updateEmployeeById(employee);
    }

    //获取id  +1 赋值给新进员工
    @Override
    public Integer maxWorkID() {
        return employeeMapper.maxWorkID();
    }

    @GetMapping("/positions")
    @ApiOperation(value = "查询出所有的职位")
    public List<Position> positions(){
        return positionService.list();
    }


    //导出数据为Excel
    @Override
    public List<EmployeeDataBase> QueryAllEmployee() {
        return employeeMapper.QueryAllEmployee();
    }


/*    //导入Excel数据
    public void insertAll(List<Employee> list) {
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
        employeeMapper.insertEmployeeDataBases(list);
    }*/
    //导入Excel数据
    public Integer employeeCount(){
        Integer count = employeeMapper.employeeCount();
        return count;
    }




}
